// pages/cate/cate.js

import {
    getNavigationList,
    getProductionList
} from "../../api/apis"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        navigationActiveIndex: 0,
        navigationList: [],
        productionList: [],
        isLoading: false,
        isNoData: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        let {index} = options
        if (index != null) {
            this.setData({
                navigationActiveIndex: Number(index)
            })
        }
        await this.getNavigationListData()
        await this.getProductionListData()
    },

    async getNavigationListData() {
        let res = await getNavigationList()
        this.setData({
            navigationList: res.data
        })
        this.selectComponent("#myTabs").resize()
    },

    async getProductionListData(size=0) {
        this.setData({
            isLoading: true
        })
        let res = await getProductionList({
            navid: this.data.navigationList[this.data.navigationActiveIndex]._id,
            limit: 5,
            size: size
        })
        let oldData = this.data.productionList
        let newData = oldData.concat(res.data)
        this.setData({
            productionList: newData,
            isLoading: false
        })
        if (this.data.productionList.length == res.total) {
            this.setData({
                isNoData: true
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if (this.data.isNoData || this.data.isLoading) return
        this.getProductionListData(this.data.productionList.length)
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    onTabsClick(event) {
        let index = event.detail.index
        this.setData({
            navigationActiveIndex: Number(index),
            productionList: [],
            isNoData: false,
            isLoading: false
        })
        this.getProductionListData()
    }
})